package com.example.event_resource_java.authority.server.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.event_resource_java.authority.dao.JzChargeRegion;
import com.example.event_resource_java.authority.mapper.JzChargeRegionMapper;
import com.example.event_resource_java.authority.server.JzChargeRegionService;
import com.example.event_resource_java.commons.vo.ChargeRegionVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源库区域负责人表 服务实现类
 * </p>
 *
 * @author ak
 * @since 2024-11-20
 */
@Service
@RequiredArgsConstructor
public class JzChargeRegionServiceImpl extends ServiceImpl<JzChargeRegionMapper, JzChargeRegion> implements JzChargeRegionService, IService<JzChargeRegion> {

    private final JzChargeRegionMapper jzChargeRegionMapper;

    @Override
    public List<ChargeRegionVO> queryList() {
        List<JzChargeRegion> jzChargeRegions = jzChargeRegionMapper.selectList(new QueryWrapper<>());

        ArrayList<ChargeRegionVO> regionArrayList = new ArrayList<>();
        jzChargeRegions.stream().forEach(r -> {
            String province = r.getProvince();
            String city = r.getCity();
            String district = r.getDistrict();
            String area = province + "," + city + "," + district;
            String[] split = area.split(",");

            for (String s : split) {
                ChargeRegionVO chargeRegionVO = new ChargeRegionVO();
                chargeRegionVO.setQuestion(s);
                chargeRegionVO.setAnswer(r.getName() + "，" + r.getPhonenumber());
                chargeRegionVO.setQRcode(r.getQRcode());
                chargeRegionVO.setRemark(r.getRemark());
                chargeRegionVO.setProvince(province);
                regionArrayList.add(chargeRegionVO);
            }
        });

        return regionArrayList;
    }

    @Override
    public HashSet<ChargeRegionVO> search(String question) {
        Assert.notEmpty(question, "问题不能为空!");

        // 切割省市区格式地址
        String province = "", city = "", district = "", counties = "";
        int provinceIndex = question.indexOf("省") + 1;
        int cityIndex = question.indexOf("市") + 1;
        int districtIndex = question.indexOf("区") + 1;
        int countiesIndex = question.indexOf("县") + 1;
        if (cityIndex < 0) {
            cityIndex = question.indexOf("州") + 1;
        }
        if (provinceIndex > 0) {
            province = question.substring(0, provinceIndex);
        }

        if (cityIndex > 0) {
            if (provinceIndex > 0) {
                city = question.substring(provinceIndex, cityIndex);
            } else {
                city = question.substring(0, cityIndex);
            }
        }
        if (districtIndex > 0) {
            if (cityIndex > 0) {
                if (cityIndex < districtIndex) {
                    district = question.substring(cityIndex, districtIndex);
                }else {
                    district = question.substring(districtIndex, cityIndex);
                }

            } else if (provinceIndex > 0) {
                district = question.substring(provinceIndex, districtIndex);
            } else {
                district = question.substring(0, districtIndex);
            }
        }
        if (countiesIndex > 0) {
            if (cityIndex > 0) {
                counties = question.substring(cityIndex, countiesIndex);
            } else {
                counties = question.substring(0, countiesIndex);
            }
        }

        if (district.equals("新疆维吾尔自治区") || district.equals("宁夏回族自治区") || district.equals("西藏自治区")
                || district.equals("广西壮族自治区") || district.equals("内蒙古自治区")) {
            province = district;
        }

        ArrayList<JzChargeRegion> regionArrayList = new ArrayList<>();
        //直接查询
        QueryWrapper<JzChargeRegion> provinceWrapper = new QueryWrapper<JzChargeRegion>()
                .like("province", question);
        regionArrayList.addAll(jzChargeRegionMapper.selectList(provinceWrapper));

        QueryWrapper<JzChargeRegion> cityWrapper = new QueryWrapper<JzChargeRegion>()
                .like("city", question);
        regionArrayList.addAll(jzChargeRegionMapper.selectList(cityWrapper));

        QueryWrapper<JzChargeRegion> districtWrapper = new QueryWrapper<JzChargeRegion>()
                .like("district", question);
        regionArrayList.addAll(jzChargeRegionMapper.selectList(districtWrapper));

        //省市区县分开查询
        if (StrUtil.isNotEmpty(province)) {
            QueryWrapper<JzChargeRegion> provinceWrapper2 = new QueryWrapper<JzChargeRegion>()
                    .like("province", province);
            regionArrayList.addAll(jzChargeRegionMapper.selectList(provinceWrapper2));
        }

        if (StrUtil.isNotEmpty(city)) {
            QueryWrapper<JzChargeRegion> cityWrapper2 = new QueryWrapper<JzChargeRegion>()
                    .like("city", city);
            regionArrayList.addAll(jzChargeRegionMapper.selectList(cityWrapper2));
        }

        if (StrUtil.isNotEmpty(district)) {
            QueryWrapper<JzChargeRegion> districtWrapper2 = new QueryWrapper<JzChargeRegion>()
                    .like("district", district);
            regionArrayList.addAll(jzChargeRegionMapper.selectList(districtWrapper2));
        }

        if (StrUtil.isNotEmpty(counties)) {
            QueryWrapper<JzChargeRegion> districtWrapper2 = new QueryWrapper<JzChargeRegion>()
                    .like("counties", counties);
            regionArrayList.addAll(jzChargeRegionMapper.selectList(districtWrapper2));
        }

        // 返回处理
        List<ChargeRegionVO> result = regionArrayList.stream().map(r -> {
            ChargeRegionVO chargeRegionVO = new ChargeRegionVO();
            chargeRegionVO.setQuestion(question);
            chargeRegionVO.setAnswer(r.getName() + " " + r.getPhonenumber());
            chargeRegionVO.setQRcode(r.getQRcode());
            chargeRegionVO.setRemark(r.getRemark());
            chargeRegionVO.setProvince(r.getProvince());
            if (r.getProvince().contains(question)) {
                chargeRegionVO.setCity(r.getCity());
            }
            if (r.getCity().contains(question)) {
                chargeRegionVO.setDistrict(r.getDistrict());
            }
            return chargeRegionVO;
        }).collect(Collectors.toList());
        // 清除重复数据
        HashSet<ChargeRegionVO> chargeRegionVOHashSet = new HashSet<>(result);
        return chargeRegionVOHashSet;
    }

//    private static void addressSplit(String question, String province, String city, String district) {
//        int provinceIndex = question.indexOf("省") + 1;
//        int cityIndex = question.indexOf("市") + 1;
//        int districtIndex = question.indexOf("区") + 1;
//        int countiesIndex = question.indexOf("县") + 1;
//        if (provinceIndex > 0) {
//            province = question.substring(0, provinceIndex);
//        }
//
//        if (cityIndex > 0) {
//            if (provinceIndex > 0) {
//                city = question.substring(provinceIndex, cityIndex);
//            } else {
//                city = question.substring(0, cityIndex);
//            }
//        }
//        if (districtIndex > 0) {
//            if (cityIndex > 0) {
//                district = question.substring(cityIndex, districtIndex);
//            } else if (provinceIndex > 0) {
//                district = question.substring(provinceIndex, districtIndex);
//            } else {
//                district = question.substring(0, districtIndex);
//            }
//        }
//        if (countiesIndex > 0) {
//            if (cityIndex > 0) {
//                String counties = question.substring(cityIndex, countiesIndex);
//            } else {
//                String counties = question.substring(0, countiesIndex);
//            }
//        }
//
////        if (district.equals("新疆维吾尔自治区") || district.equals("宁夏回族自治区") || district.equals("西藏自治区")
////                || district.equals("广西壮族自治区") || district.equals("内蒙古自治区")) {
////            province = district;
////        }
//    }
}